#ifndef MATCHING_H
#define MATCHING_H

#pragma once

#include "cv.h"
#include "View.h"
#include "Match.h"
#include "cvaux.h"


class Matching
{

public:

    // define the size of search region
	int searchRegionWidth;
	int searchRegionHeight;

	// define the the correlation window size
	int correlationWindowWidth;
	int correlationWindowHeight;
	
	bool isMatchPointsLoaded;

	// Grade (or numbers of windows the image will be divided)
	int rows;
	int colluns;

	double sizeMultplier;// multiply (raises) the the template search region 

	// store all the point correspondences (used to reconstruction)
	  int matchPointsNumber;
	  vector<CvPoint> matchPointsScene1; // if stereo (left)
	  vector<CvPoint> matchPointsScene2; // if stereo (right)

	
	Matching(void);
	~Matching(void);

	// Set the rect size auround of any point, this will be the search region for a match in the second image
	void setSearchRegionSize(int width, int height);

	// Configure most part of paramaters
	// @ parameters
	// rowsgrade and collunsgrade - define the grade of windows for extract corners in each image
	// correlationWindowW and correlationWindowH - size of window used to correlation method (in pixels)
	// cornersSearchRegionW and cornersSearchRegionH - size of region for corners matchs searching 
	// matchPointsNum - number of points in each window (defined by the grade, parameters 1 and 2)
	void setParameters(int rowsgrade, int collunsgrade, int correlationWindowW, int correlationWindowH, int cornersSearchRegionW, int cornersSearchRegionH, int matchPointsNum);
	
	// Set the window size for correlation betwen two image points
	void setCorrelationWindowSize(int width, int height);
	
	// Set the interestPoints array of any View with all the points inside the search region according to the SearchRegionSize defined
	void setPointsInSearchRegion(CvPoint pointImg1, View &view);
	
	// Get the best Match of a image  betwen this point (window) and all the points in the search region of second View
	Match& findBestMatch(CvPoint pointImg1, View &view1,View &view2);
	
	// Returns the Max value of correlation betwen img and tpl images
	double maxCorrelation(IplImage &img, IplImage &tpl);
    
	// Mark the Match in the 2 Scenes
	void markMatch(View &view1, View &view2, Match &match);

	// Find all the matches in each cell of the image and store the correspondence points in pointsScene1 and pointsScene2 
	void Matching::findAllBestMatches(View &view1,View &view2,int row, int col, int numpoints);

	// draw the lines of Match moviments betwen the point1 and its correspondent point2 
	void Matching::drawLines(View &view1);

	// usus Ransac method to eliminate outliers
	void Matching::RefineMatching(int ntimes);

	// Mark all the match points found with numbers, in both images
	void Matching::markMatchsWithNumbers(View &view1, View &view2,int numberPoints);

	void Matching::MarkSearchRegion(CvPoint point,View &view);


};
#endif


